<?php 
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

class JAJobBoardControllerJAFeeds extends JAJobBoardController {
/* Constructor
     */
	var $_link = null;
    function __construct( $location = array() )
    {
    	
        parent::__construct( $location );      
        // Register Extra tasks
        $this->registerTask( 'add',     'edit' );
        $this->registerTask( 'apply',   'save' );        
//		$this->registerTask( 'unpublish', 	'publish');
		$is_user = JRequest::getVar('is_user');
		$tabs = $is_user?'userlist':'feedlist';
		$this->_link = "index.php?option=com_jajobboard&view=jafeeds&tab=$tabs";
    }
    
    function display()
    {
    	parent::display();
    }

	function cancel()
	{
		
		//$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( $this->_link );
	}
	
	function edit()
	{
		
		JRequest::setVar( 'view', 'jafeeds' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}
	
	function save()
	{
		$model = $this->getModel('jafeeds');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($cid, array(0));
		if ($id = $model->store()) {
			if (($cid[0]!=0)&&(isset($cid[0])))
				$msg = JText::_( 'Successfully updated feed' );
			else 
				$msg = JText::_( 'Successfully created feed' );
				
			if($this->_task == 'apply')
			{
				//die(JRequest::getVar('id' ));
				$this->_link = "index.php?option=com_jajobboard&view=jafeeds&task=edit&cid[]=$id";
			}
			$this->setRedirect( $this->_link, $msg );				
		} else {
			$msg = JText::_( 'Error found!Fail to save feed' );
			JRequest::setVar('task','edit');
			JRequest::setVar('postback',true);
			JRequest::setVar('cid[]',$cid[0]);
			parent::display();
		}
		
	}

	function publish()
	{
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('jafeeds');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect($this->_link);
	}
	
	function unpublish()
	{
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('jafeeds');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect($this->_link);
	}

	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('jafeeds');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect($this->_link);
	}    
}

?>